<!--
 * @Description: file content
 * @Author: oooh
 * @Date: 2024-04-26 11:16:35
 * @LastEditors: oooh
 * @LastEditTime: 2024-08-02 10:42:46
 * @FilePath: \Civil_Servant_Performance_PC\src\views\work-approval\evaluation-comment\discipline-inspection\index.vue
-->
<template>
  <div class="app-container">
    <el-row>
      <el-col>
        <el-select v-model="queryParams.year" placeholder="选择年份" class="ml-10" @change="handleYearChange">
          <el-option v-for="item in dict.type.biz_work_year_list" :key="item.value" :label="item.label" :value="item.value"> </el-option>
        </el-select>
        <el-select v-model="queryParams.quarter" placeholder="选择季度" class="ml-10" @change="handleQuarterChange">
          <el-option v-for="item in quarterOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
        </el-select>
      </el-col>
    </el-row>
    <div class="text-banner">
      <p>
        <span>服务效能评价</span>
        <span style="margin-left: 30px; font-size: 16px; font-weight: normal">
          <i class="el-icon-warning"></i>
          出现一例扣0.1分，不重复扣分
        </span>
      </p>
      <el-button class="ml-10" size="mini" type="primary" circle icon="el-icon-refresh" plain @click="handleRefresh" />
    </div>
    <div class="flex justify-between align-center mb-10">
      <div class="el-upload__tip">
        <p>
          <b style="color: #f56c6c">*佐证材料：</b>大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> 格式为
          <b style="color: #f56c6c">{{ fileType.join('/') }}</b
          >的文件（不超过<b style="color: #f56c6c">{{ limit }}</b
          >项）
        </p>
      </div>
      <div>
        <el-button
          icon="el-icon-s-claim"
          size="mini"
          type="primary"
          plain
          @click="handleSubmit"
          v-hasPermi="['evaluation-comment:discipline-inspection:submit']"
          >保存并提交</el-button
        >
      </div>
    </div>

    <div class="table">
      <el-table :data="workList" border style="width: 100%" height="100%" v-loading="loading">
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="deptName" label="站办所" width="180" align="center" show-overflow-tooltip> </el-table-column>
        <el-table-column v-for="(item, index) in columns" :key="item.value" :label="item.label" min-width="120" align="center">
          <template slot-scope="scope">
            <el-input-number v-model="scope.row.normList[index].total" placeholder="数量" controls-position="right" :min="0"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column prop="otherScore" label="其他得分" width="150" align="center">
          <template slot-scope="scope">
            <el-input-number v-model="scope.row.otherScore" placeholder="其他得分" controls-position="right"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column prop="materialUrls" label="佐证材料" min-width="180" align="center">
          <template slot-scope="scope">
            <FileUpload v-model="scope.row.materialUrls" :isShowTip="false" :fileType="fileType"></FileUpload>
          </template>
        </el-table-column>
        <el-table-column prop="add" label="加分" width="70" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.otherScore > 0 ? numberParse(scope.row.otherScore, 1) : 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="reduce" label="扣分" width="70" align="center">
          <template slot-scope="scope">
            <span>{{ rowScoreComputeReduce(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="总得分" align="center">
          <template slot-scope="scope">
            <span>{{ rowScoreCompute(scope.row) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { list, edit } from '@/api/work-approval/evaluation-comment/discipline-inspection'
import { quarterFormat } from '@/utils/quarter.js'
import { getDicts } from '@/api/system/dict/data.js'
import FilePreview from '@/components/FilePreview'
import FileUpload from '@/components/FileUpload'
import { mixins } from '@/mixins'
export default {
  name: 'evaluation-comment_discipline-inspection',
  mixins: [mixins],
  dicts: ['biz_work_year_list'],
  props: {},
  components: { FilePreview, FileUpload },
  data() {
    return {
      filePreviewVisible: false,
      previewFile: {},
      columns: [],
      workMaxScore: 0, // 纪检办评价评议分数
      loading: false,
      quarterOptions: [],
      workList: [],
      queryParams: {
        year: '',
        quarter: ''
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getWorkScore()
    this.handleYearChange(new Date().getFullYear())
  },
  methods: {
    // 预览文件
    handlePreviewFile(url) {
      let fileItem = this.formatFileUrl(url)
      this.previewFile = { url, ...fileItem }
      this.filePreviewVisible = true
    },
    // 扣分计算
    rowScoreComputeReduce(raw) {
      let totalScore = raw.normList.reduce((prev, curr) => {
        let score = curr.total
        if (!curr.total) score = 0
        return prev + score
      }, 0)
      totalScore *= -0.1
      if (raw.otherScore < 0) {
        totalScore = totalScore + raw.otherScore
      }
      totalScore = this.numberParse(totalScore, 1)
      return totalScore
    },
    // 表格行扣分计算
    rowScoreCompute(raw) {
      let totalScore = raw.normList.reduce((prev, curr) => {
        let score = curr.total
        if (!curr.total) score = 0
        return prev + score
      }, 0)
      totalScore *= -0.1
      totalScore = totalScore + raw.otherScore || 0
      totalScore = this.numberParse(totalScore, 1)
      return totalScore
    },
    // 保存并提交评价评议
    handleSubmit() {
      let params = this.workList.map(item => {
        let normList = []

        for (let normalItem of item.normList) {
          if (normalItem.total === '' || normalItem.total === null) normalItem.total = 0
          normList.push({ normDict: normalItem.normDict, total: normalItem.total })
        }
        return { ...this.queryParams, otherScore: item.otherScore, deptId: item.deptId, normList, materialUrls: item.materialUrls }
      })
      edit(params).then(res => {
        this.$modal.msgSuccess('已提交')
        this.handleRefresh()
      })
    },
    getList() {
      this.loading = true
      list(this.queryParams)
        .then(res => {
          this.loading = false
          this.workList = res.data
          this.columns = res.data[0]['normList'].map(item => {
            return { label: item.normLabel, value: item.normDict }
          })
        })
        .catch(err => (this.loading = false))
    },
    // 刷新
    handleRefresh() {
      this.getList()
    },
    // 年度选择
    handleYearChange(year) {
      this.queryParams.year = year
      // 获取当前季度、季度列表（往年默认第一季度）
      let { quarter, quarterList } = quarterFormat(year)
      this.quarterOptions = quarterList
      this.handleQuarterChange(quarter)
    },
    // 季度选择
    handleQuarterChange(quarter) {
      this.queryParams.quarter = quarter
      this.handleRefresh()
    },

    // 获取工作分数限制
    getWorkScore() {
      getDicts('biz_dept_norm_config').then(res => {
        let { data } = res
        this.workMaxScore = data.find(item => item.dictValue === 'JJB_PJPY')?.remark
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-input-number--medium {
  width: auto;
}
.app-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .table {
    flex: 1;
    overflow-y: hidden;
  }
}
</style>
